package com.quanshu.exchange.ui.gui.profile;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.entity.SystemMessageEntity;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 官方消息 or 其他消息
 * Created by xiniu003 on 2018/1/12.
 */

public class OfficialMassageActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.official_massage_title)
    TextView officialMassageTitle;
    @BindView(R.id.official_massage_no_llt)
    LinearLayout officialMassageNoLlt;
    @BindView(R.id.official_massage_recy)
    RecyclerView officialMassageRecy;

    private List<SystemMessageEntity> massageList = new ArrayList<>();
    private SingleAdapter<SystemMessageEntity> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_official_massage);
        ButterKnife.bind(this);

        getIntentData();
        initListener();
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
    }

    private void getIntentData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("massage");
        officialMassageTitle.setText(title);

        massageList = (List<SystemMessageEntity>) intent.getSerializableExtra("officialMassage");
        if (massageList != null && massageList.size() > 0) {
            initAdapter();
            adapter.setData(massageList);
            officialMassageNoLlt.setVisibility(View.GONE);
        } else {
            officialMassageNoLlt.setVisibility(View.VISIBLE);
        }

    }

    private void initAdapter() {
        officialMassageRecy.setLayoutManager(new LinearLayoutManager(OfficialMassageActivity.this));
        adapter = new SingleAdapter<>(new DataDisplayer<SystemMessageEntity>(OfficialMassageActivity.this,
                R.layout.item_sys_massage) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<SystemMessageEntity> wrapper,
                                 SystemMessageEntity item,
                                 int position) {

                holder.setText(R.id.sys_massage_title, item.getTitle());
                holder.setText(R.id.sys_massage_time, item.getCreate_time());
                holder.setText(R.id.sys_massage_content, item.getContent());
            }
        });
        officialMassageRecy.setAdapter(adapter);
    }
}
